home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue32 / clinic / LAUNCH1U.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-01-06  |  3.4 KB  |  130 lines

  1. unit Launch1U;
  2.  
  3. {$ifdef Win32}
  4.   'This one is for Windows 3.1x & Delphi 1'
  5. {$endif}
  6.  
  7. interface
  8.  
  9. uses
  10.   WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  11.   Dialogs, StdCtrls, ExtCtrls;
  12.  
  13. type
  14.   TForm1 = class(TForm)
  15.     dlgOpen: TOpenDialog;
  16.     btnRefresh: TButton;
  17.     lstWindows: TListBox;
  18.     Timer1: TTimer;
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.     procedure btnRefreshClick(Sender: TObject);
  22.   public
  23.     Wnd: HWnd;
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. uses
  32.   ToolHelp;
  33.  
  34. {$R *.DFM}
  35.  
  36. procedure DescribeWnd(Wnd: HWnd; List: TStrings);
  37. const
  38.   FlagStr: array[Boolean] of String = ('not ', '');
  39. var
  40.   CString: array[0..255] of Char;
  41.   Rect: TRect;
  42. begin
  43.   with List do
  44.   begin
  45.     BeginUpdate;
  46.     try
  47.       { Check the window is valid }
  48.       if not IsWindow(Wnd) then
  49.       begin
  50.         Add('Window is not valid');
  51.         Abort
  52.       end;
  53.       Add(Format('Window handle = $%x', [Wnd]));
  54.       { Get window caption }
  55.       GetWindowText(Wnd, CString, SizeOf(CString));
  56.       Add(Format('Window caption is "%s"', [CString]));
  57.       { get window dimensions }
  58.       GetWindowRect(Wnd, Rect);
  59.       with Rect do
  60.         Add(Format('Window co-ordinates are (%d,%d) - (%d,%d)',
  61.           [Left, Top, Right, Bottom]));
  62.       Add(Format('Window is from app with instance handle of $%x',
  63.         [GetWindowWord(Wnd, gww_HInstance)]));
  64.       { Get thread id & process id of owning app }
  65.       Add(Format('Window is from app with task handle of $%x',
  66.         [GetWindowTask(Wnd)]));
  67.       Add(Format('Window has ID of $%x', [GetWindowWord(Wnd, gww_ID)]));
  68.       Add(Format('The window is %svisible', [FlagStr[IsWindowVisible(Wnd)]]));
  69.       Add(Format('The window is %senabled', [FlagStr[IsWindowEnabled(Wnd)]]));
  70.       Application.Icon.Handle := GetClassWord(Wnd, gcw_HIcon);
  71.       Add('The window''s icon has been set as this application''s icon');
  72.       Add('The window''s caption bar should be flashing')
  73.     finally
  74.       EndUpdate
  75.     end
  76.   end
  77. end;
  78.  
  79. function EnumFunc(Wnd: HWnd; PWnd: PHandle): Bool; far;
  80. begin
  81.   Result := False;
  82.   PWnd^ := Wnd
  83. end;
  84.  
  85. procedure TForm1.FormCreate(Sender: TObject);
  86. var
  87.   CString: array[0..255] of Char;
  88.   Inst, Task: THandle;
  89.   TE: TTaskEntry;
  90. begin
  91.   { Set open dialog to look in Windows directory }
  92.   GetWindowsDirectory(CString, SizeOf(CString));
  93.   dlgOpen.InitialDir := StrPas(CString);
  94.   if dlgOpen.Execute then
  95.   begin
  96.     { Launch chosen app }
  97.     StrPCopy(CString, dlgOpen.FileName);
  98.     Inst := WinExec(CString, sw_ShowNormal);
  99.     { Turn instance handle into task handle }
  100.     FillChar(TE, SizeOf(TE), 0);
  101.     TE.dwSize := SizeOf(TE);
  102.     if TaskFirst(@TE) then
  103.       repeat
  104.         if TE.hInst = Inst then
  105.           Task := TE.hTask
  106.         else if TE.hModule = Inst then
  107.           Task := TE.hTask
  108.       until (TE.hInst = Inst) or (TE.hModule = Inst) or not TaskNext(@TE);
  109.     { Loop thru all task's windows }
  110.     EnumTaskWindows(Task, @EnumFunc, Longint(@Wnd));
  111.     { Fill listbox with info on the main window }
  112.     DescribeWnd(Wnd, lstWindows.Items);
  113.   end
  114. end;
  115.  
  116. procedure TForm1.Timer1Timer(Sender: TObject);
  117. begin
  118.   { Flash the window's caption bar }
  119.   if IsWindow(Wnd) then
  120.     FlashWindow(Wnd, True)
  121. end;
  122.  
  123. procedure TForm1.btnRefreshClick(Sender: TObject);
  124. begin
  125.   lstWindows.Clear;
  126.   DescribeWnd(Wnd, lstWindows.Items);
  127. end;
  128.  
  129. end.
  130.